﻿#include "hoteldetails.h"
#include "ui_hoteldetails.h"
#include <QDate>
#include <QMessageBox>

HotelDetails::HotelDetails(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HotelDetails)
{
    ui->setupUi(this);
    setTime();

    m_reservehotel = nullptr;
    m_hotelDetailsInfo = nullptr;
    m_contentCount = 0;
    m_houseType.push_back("酒店");
    m_imageCount.push_back(0);
    QImage image;
    QList<QImage> imageList;
    imageList.push_back(image);
    m_imageList.push_back(imageList);
}

HotelDetails::HotelDetails(const HotelInfo& info, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HotelDetails)
{
    ui->setupUi(this);
    setTime();

    m_reservehotel = nullptr;
    m_hotelDetailsInfo = nullptr;
    m_contentCount = 0;
    m_houseType.push_back("酒店");
    m_imageCount.push_back(0);
    QImage image;
    QList<QImage> imageList;
    imageList.push_back(image);
    m_imageList.push_back(imageList);

    initWindowsUnits(info);
}

HotelDetails::~HotelDetails()
{
    delete ui;
}

void HotelDetails::initWindowsUnits(const HotelInfo& info)
{
    m_hotelInfo = info;
    m_hoteldetailsheader = new HotelDetailsHeader();
    m_hoteldetailsheader->setHotelInfo(m_hotelInfo);
    ui->lwg_content->addItem("");
    ui->lwg_content->setItemWidget(ui->lwg_content->item(0),m_hoteldetailsheader);
    ui->lwg_content->item(0)->setSizeHint(QSize(0, 386));  //每次改变Item的高度

    connect(m_hoteldetailsheader,SIGNAL(signalSubmitSave(bool)),
            this,SIGNAL(signalSubmitSaveHotel(bool)));
    connect(m_hoteldetailsheader,SIGNAL(signalJumpToComment()),
            this,SLOT(slotJumpToComment()));

}

void HotelDetails::slotJumpToComment()
{
    qDebug() << "jump";
    if (m_hotelComment != nullptr)
    {
        if (ui->lwg_content->currentRow() == m_contentCount)
        {
            ui->lwg_content->setCurrentRow(0);
        }
        ui->lwg_content->setCurrentRow(m_contentCount);
    }
}

/// 设置酒店详情的图片
void HotelDetails::slotHotelDetailsImage()
{
//    m_hoteldetailsheader->setImage(GlobalVars::g_hotelImageList);
}

/// 预定酒店  /// 这里修改  照片
void HotelDetails::slotReserveHotel(const HouseInfo &houseInfo)
{
    m_reservehotel = new ReserveHotel();
    m_reservehotel->setWindowTitle("预定酒店");
    m_reservehotel->setWindowModality(Qt::ApplicationModal);
    m_reservehotel->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_reservehotel->setFixedSize(m_reservehotel->width(),m_reservehotel->height());                                         // 禁止拖动窗口大小
    m_reservehotel->setHotel(m_hotelInfo);
    m_reservehotel->setHouse(houseInfo);
    m_reservehotel->setReserveTime(m_inTime,m_outTime);
    for (int i = 0; i < m_houseType.size(); i++)
    {
        if (m_houseType[i] == "酒店")
        {
            m_reservehotel->setImage(m_imageList[1][0]);
            break;
        }
    }
    m_reservehotel->show();

    connect(m_reservehotel,SIGNAL(signalSubmitReserve()),
            this,SIGNAL(signalSubmitReserveHotel()));
}

void HotelDetails::slotSubmitReserveResult(bool result, const QString& msg)
{
    m_reservehotel->submitReserveResult(result,msg);
}

void HotelDetails::slotSetHouseList(bool result, QString msg)
{
    if (result)
    {
        m_contentCount++;
        HotelDetailsContent *hoteldetailsContent = new HotelDetailsContent();
        m_hotelDetailsContentVector.push_back(hoteldetailsContent);
        ui->lwg_content->addItem("");
        ui->lwg_content->setItemWidget(ui->lwg_content->item(m_contentCount),hoteldetailsContent);
        ui->lwg_content->item(m_contentCount)->setSizeHint(QSize(0, 160));  //每次改变Item的高度

        connect(hoteldetailsContent,SIGNAL(signalReserveHotel(HouseInfo)),
                this, SLOT(slotReserveHotel(const HouseInfo &)));
    }
    else
    {
        qDebug() << "房间" << msg;
        if (msg == "没有下一条记录")
        {
            setHotelDetailsInfoShow();

            m_contentCount++;
            m_hotelComment = new HotelDetailsComment();
            ui->lwg_content->addItem("");
            ui->lwg_content->setItemWidget(ui->lwg_content->item(m_contentCount),m_hotelComment);
            ui->lwg_content->item(m_contentCount)->setSizeHint(QSize(0, 890));  //每次改变Item的高度
        }
    }

}

void HotelDetails::slotSubmitSaveResult(bool result, const QString& msg)
{
    m_hoteldetailsheader->setSubmitSaveResult(result,msg);
}

void HotelDetails::cleanHotelDetails()
{
    while (ui->lwg_content->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_content->takeItem(0);
        delete item;
    }

    m_contentCount = 0;
    for (int i = 0; i < m_houseType.size(); i++)
    {
        m_imageCount[i] = 0;
    }
    m_houseType.clear();
    m_imageList.clear();
    GlobalVars::g_hotelImageList.clear();
    GlobalVars::g_houseImageList.clear();
    m_hotelDetailsContentVector.clear();
    if (m_reservehotel != nullptr)
    {
        delete m_reservehotel;
        m_reservehotel = nullptr;
    }

    m_houseType.push_back("酒店");
    m_imageCount.push_back(0);
    QImage image;
    QList<QImage> imageList;
    imageList.push_back(image);
    m_imageList.push_back(imageList);
}

/// image
/// m_contentCount = 房间的类型数
/// 房间类型   照片个数  图片列表--> 二维数组
void HotelDetails::slotSaveHouseImage(const QString& type, const QImage& image)
{
    qDebug() << "saveHouseImage";
    if (!m_houseType.contains(type))
    {
        m_houseType.push_back(type);
        m_imageCount.push_back(1);
        QList<QImage> imageList;
        imageList.push_back(image);
        m_imageList.push_back(imageList);
    }
    else{
        int index = m_houseType.indexOf(type);
        qDebug() << "m_imageList" << m_imageList;
        qDebug() << "m_imageCount " << m_imageCount;
        qDebug() << "index " << index;
        m_imageCount[index]++;
        m_imageList[index].push_back(image);
    }
    qDebug() << "saveHouseImage  --  end";
}

/// 这里修改  照片
void HotelDetails::slotSetHouseImage()
{
    qDebug() << "slotSetHouseImage" << m_houseType;
    for (int i = 0; i < m_houseType.size(); i++)
    {
        if (m_houseType[i] == "酒店")
        {
            m_hoteldetailsheader->setImage(GlobalVars::g_hotelImageList + GlobalVars::g_houseImageList);///m_imageList[1]
            continue;
        }
        for (int j = 0; j < m_hotelDetailsContentVector.size(); j++)
        {
            if (m_houseType[i] == m_hotelDetailsContentVector.at(j)->getType())
            {
                m_hotelDetailsContentVector[j]->setImage(m_imageCount[i],m_imageList[i].at(0));
                break;
            }
        }
    }
}

void HotelDetails::setHotelDetailsInfoShow()
{
    m_contentCount++;
    m_hotelDetailsInfo = new HotelDetailsInfo();
    m_hotelDetailsInfo->initWindowsUnit(m_hotelInfo);
    ui->lwg_content->addItem("");
    ui->lwg_content->setItemWidget(ui->lwg_content->item(m_contentCount),m_hotelDetailsInfo);
    ui->lwg_content->item(m_contentCount)->setSizeHint(QSize(0, 250));  //每次改变Item的高度
}

void HotelDetails::setTime(const QString& inTime, const QString& outTime)
{
    m_inTime = inTime;
    m_outTime = outTime;
}

void HotelDetails::setTime()
{
    m_inTime = QDate::currentDate().toString("yyyy-MM-dd");
    m_outTime = QDate::currentDate().addDays(1).toString("yyyy-MM-dd");;
}

void HotelDetails::slotSetCommentList(bool result, const QString &msg)
{
    if (result)
    {
        m_hotelComment->setCommentContent();
    }
    else
    {
        qDebug() << "房间" << msg;
    }
}

void HotelDetails::slotSetCommentImage(bool result,const QString& msg,const QImage& image)
{
    if (result)
    {
        m_hotelComment->setCommentImage(msg,image);
    }
    else
    {
        qDebug() << "评论图片" << msg;
    }
}

